package com.lora.service.resolver;

import com.lora.dto.EquipmentDto;
import com.lora.entity.EntranceGuard;
import com.lora.service.EntranceGuardService;
import com.lora.service.NodeDataResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.xml.ws.Action;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component("entranceGuardResolver")
public class EntranceGuardResolver implements NodeDataResolver<EntranceGuard> {

    @Resource
    private EntranceGuardService entranceGuardService;

    @Override
    public EquipmentDto<EntranceGuard> resolve(String equipmentNumber, String data) {
        EquipmentDto<EntranceGuard> equipmentData = new EquipmentDto<>();
        Map<String,String> map=new HashMap<>();
        String[] split = data.split("\r\n");
        for (String s : split) {
            String[] split1 = s.split(": ");
            if(split1.length==2)
                map.put(split1[0],split1[1]);
        }
        EntranceGuard entranceGuard=new EntranceGuard();
        entranceGuard.setAddress(map.get("地点"));
        entranceGuard.setRec(map.get("Rec"));
        entranceGuard.setReasonno(map.get("ReasonNo"));
        entranceGuard.setCardnum(map.get("卡号"));
        entranceGuard.setJobnumber(map.get("工号"));
        entranceGuard.setName(map.get("姓名"));
        entranceGuard.setDepartment(map.get("部门"));
        entranceGuard.setTime(map.get("时间"));
        entranceGuard.setState(map.get("状态"));
        entranceGuard.setWay(map.get("进出"));
        equipmentData.setEntity(entranceGuard);
        return equipmentData;
    }

    @Override
    public EntranceGuard save(EntranceGuard equipment, boolean alarmFlag) {
        entranceGuardService.save(equipment);
        return equipment;
    }
}
